package zhh.tangbao.commodity.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhh.tangbao.commodity.dto.CommodityOrderQueryDto;
import zhh.tangbao.commodity.entity.CommodityOrder;
import zhh.tangbao.commodity.serivce.CommodityOrderService;
import zhh.tangbao.system.annotation.Authority;
import zhh.tangbao.system.annotation.Log;
import zhh.tangbao.system.constant.AuthorityModel;
import zhh.tangbao.system.result.Result;
import zhh.tangbao.system.vo.SelectOptionsVo;

import java.util.List;

/**
 * <p>
 *     描述
 * </p>
 * @author 粥灰灰
 * <p>2023/3/5</p>
 */

@RestController
@RequestMapping("/api/order")
public class CommodityOrderController {

    @Autowired
    private CommodityOrderService service;

    @GetMapping("/get/{id}")
    @Authority(login = false)
    public Result get(@PathVariable("id") String id) {
        return Result.success().put(service.get(id));
    }

    @GetMapping("/page")
    @Authority(login = false)
    public Result page(CommodityOrderQueryDto dto) {
        return Result.success().put(service.page(dto));
    }

    /**
     * 用于管理员在系统发生故障导致客户订单未正确生成时采取的备用手段
     * 可手动生成订单
     */
    @PostMapping("/add")
    @Authority(authorities = "order", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "添加订单", moduleName = "订单管理")
    public Result add(@RequestBody CommodityOrder order) {
        return Result.successOrFail(service.add(order));
    }

    @DeleteMapping("/delete/{id}")
    @Authority(authorities = "order", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "删除订单", moduleName = "订单管理")
    public Result delete(@PathVariable("id") String id) {
        return Result.successOrFail(service.delete(id));
    }

    @PutMapping("/update")
    @Authority(authorities = "order", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "更新订单", moduleName = "订单管理")
    public Result update(@RequestBody CommodityOrder order) {
        return Result.successOrFail(service.update(order));
    }

    @GetMapping("/currentAllOrderStatusOptions")
    public Result currentAllOrderStatusOptions() {
        List<SelectOptionsVo> options = service.currentAllOrderStatusOptions();
        return Result.success().put(options);
    }

    @GetMapping("/allOrderStatusOptions")
    public Result allOrderStatusOptions() {
        List<SelectOptionsVo> options = service.allOrderStatusOptions();
        return Result.success().put(options);
    }

    @DeleteMapping("/batchDelete")
    @Authority(authorities = "order", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "批量删除订单", moduleName = "订单管理")
    public Result batchDelete(@RequestBody List<String> ids) {
        return Result.successOrFail(service.batchDelete(ids));
    }
}
